package com.easy.controller;

import com.easy.bean.Supplier;
import com.easy.service.SupplierService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    // 新增
    @PostMapping
    public Result add(@RequestBody Supplier supplier) {
        return Result.success(supplierService.add(supplier));
    }

    // 编辑
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody Supplier supplier) {
        supplier.setId(id);
        return Result.success(supplierService.edit(supplier));
    }

    // 删除
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success("删除数据", supplierService.del(id));
    }

    // 查询所有（可模糊查询）
    @GetMapping
    public Result all(
            @RequestParam(name = "name", required = false, defaultValue = "") String name
    ) {
        return Result.success(supplierService.all(name));
    }

    // 查询单个
    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        return Result.success(supplierService.one(id));
    }

    // 分页查询
    @GetMapping("page")
    public Result page(Page page, Supplier supplier){
        supplierService.page(page, supplier);
        return Result.success(page);
    }
}
